package com.tjp.eight.ui.fragment.mywallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.mywallet.details.ActivityWalletList;
import com.tjp.eight.ui.fragment.mywallet.wx.ActivityWX;
import com.tjp.eight.ui.fragment.mywallet.zfb.ActivityZFB;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/2/28.
 */
public class FragmentMyWallet extends BaseFragment<MyWalletPresenter> implements MyWalletView {
    @Bind(R.id.ll_wallet_wx)
    LinearLayout ll_wallet_wx;
    @Bind(R.id.ll_wallet_zfb)
    LinearLayout ll_wallet_zfb;
    @Bind(R.id.tv_wallet_count)
    TextView tv_wallet_count;
    @Bind(R.id.tv_min_tixian)
    TextView tv_min_tixian;

    String money = "0";

    @Override
    protected MyWalletPresenter createPresenter() {
        return new MyWalletPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);

        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_my_wallet, container, false);
        }
        ButterKnife.bind(this, contentView);
        money = getActivity().getIntent().getStringExtra("money");
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loaddata(getPJson());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private String getPJson() {
        WalletPModel model = new WalletPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    @OnClick(R.id.rl_head_left)
    void finishA() {
        AppManager.getAppManager().finishActivity(getActivity());
    }

    @OnClick(R.id.rl_head_right)
    void goWalletList() {
        Intent intent = new Intent(getActivity(), ActivityWalletList.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_wallet_wx)
    void goWX() {
        Intent intent = new Intent(getActivity(), ActivityWX.class);
        intent.putExtra("money", money);
        startActivity(intent);
    }

    @OnClick(R.id.ll_wallet_zfb)
    void goZFB() {
        Intent intent = new Intent(getActivity(), ActivityZFB.class);
        intent.putExtra("money", money);
        startActivity(intent);
    }

    void initViews() {
        tv_wallet_count.setText("" + money);
        if (Integer.parseInt(money) > 0)
            setEn(true);
        else
            setEn(false);
    }

    void setEn(boolean b) {
        ll_wallet_wx.setEnabled(b);
        ll_wallet_zfb.setEnabled(b);
    }


    @Override
    public void loadSuccess(WalletRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.if_can_tixian == 1)
                    setEn(true);
                else
                    setEn(false);

                SharePreferencesUtils.setMoney(model.money_rest+"");
                tv_wallet_count.setText("" + model.money_rest);
                tv_min_tixian.setText("最低提现金额"+model.min_tixian_money+"金币");
            } else {
                ToatUtils.toastSuccess(getContext(), "" + model.msg);
            }
        }
    }
}
